<?php
namespace app\api\controller;

use app\common\controller\Common;
use app\common\exception\VerifyCodeException;
use app\common\logic\SmsLogic;
use app\common\logic\User as UserLogic;
use think\Request;
use think\Validate;

/**
 * @title 短信接口模块
 * @description 接口说明
 * @group 接口分组
 *
 */
class Smsapi extends Common
{

    /**
     * @title 发送短信接口
     * @description 接口说明
     * @author WCJ
     * @url /api/Smsapi/send
     * @method POST
     *
     * @param name:phone type:string require:1 default:0 other: desc:电话号码
     * @param name:moban type:string require:1 default:0 other: desc:模板id（1.注册2.登陆3.转让验证）
     *
     * @return data:用户信息
     *
     */
    public function send(Request $request){
        $phone=$request->param('phone');
        $moban=$request->param('moban');
        $rule = [
            'phone' => 'number|length:11|require',
            'moban' => 'number|require',
        ];

        $msg = [
            'phone.number'     => '手机号必须是数字',
            'phone.length'     => '手机号必须是11位',
            'phone.require'     => '请输入手机号',
            'moban.number'     =>'模板参数错误',
            'moban.require'     =>'模板参数错误'
        ];

        $data = [

            'phone' => $phone,
            'moban' =>$moban
        ];

        $validate = new Validate($rule, $msg);
        $result   = $validate->check($data);
        if(!$result){
            return ['code' => 201,"msg"=>$validate->getError(),"errorCode"=>404];
            die();
        }
      
        $smslogic=new SmsLogic();
        return $smslogic->mobilesend($phone,$moban);

    }

    /**
     * @title 验证短信接口
     * @description 接口说明
     * @author WCJ
     * @url /api/Smsapi/mobileValidate
     * @method POST
     *
     * @param name:mobile type:string require:1 default:0 other: desc:电话号码
     * @param name:moban type:string require:1 default:0 other: desc:模板id（1.注册2.登陆4.内部登陆）
     * @param name:code type:string require:1 default:0 other: desc:验证码
     *
     * @return data:用户信息
     *
     */
    public function mobileValidate(Request $request){
        $mobile=$request->param('mobile');
        $moban=$request->param('moban');
        $code=$request->param('code');
        $userlogic = new UserLogic();
        //短信验证码验证
        $smsdata = $userlogic->validateCode($mobile, $code,$moban);
        if ($smsdata['code'] != 200) {
            throw new VerifyCodeException(["code" => 201, "msg" => '手机验证码错误!']);
            die();
        }else{
            return ["code"=>200,"msg"=>"验证成功"];
        }
    }





}